<?php


namespace app\common\service\companyIndustry\simulation;

use app\common\service\BaseService;
use app\common\service\FormulaService;

/**
 * 模拟数据 - 脱硝排放量
 */
class SimulationEmissionService extends BaseService
{
    // +----------------------------------------------------------------------
    // | 排口
    // +----------------------------------------------------------------------
    /**
     * 1.3 单个排口 - 排放量 - 小时平均
     * @param $drainId
     * @return array
     * @throws \Exception
     * @author hongwei 2019-07-25
     */
    public function getSimulationSingleCompanySingleDrainEmissionHourAvg($drainId)
    {
        $sql = 'SELECT drain_id, ';
        $sql .= 'sum(emissions_so2)/24 AS so2_emissions,';
        $sql .= 'sum(emissions_nox)/24 AS nox_emissions,';
        $sql .= 'sum(emissions_smoke)/24 as tsp_emissions ';
        $sql .= 'FROM `company_industry_monitor_simulation_hour_avg`  where drain_id = ' . (int)$drainId;
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetch();
        if (!$data) {
            exception('获取模拟企业小时数据失败！');
        }
        $dataNew['drain_id']      = $data['drain_id'];
        $dataNew['so2_emissions'] = $data['so2_emissions'];
        $dataNew['nox_emissions'] = $data['nox_emissions'];
        return $dataNew;
    }


    /**
     * 1.3 单个排口 - 排放量 - 天平均
     * @param $drainId
     * @return array
     * @throws \Exception
     * @author hongwei 2019-07-25
     */
    public function getSimulationSingleCompanySingleDrainEmissionDayAvg($drainId)
    {
        $sql = 'SELECT drain_id, ';
        $sql .= 'sum(emissions_so2) AS so2_emissions,';
        $sql .= 'sum(emissions_nox) AS nox_emissions,';
        $sql .= 'sum(emissions_smoke) as tsp_emissions ';
        $sql .= 'FROM `company_industry_monitor_simulation_hour_avg`  where drain_id = ' . (int)$drainId;
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetch();
        if (!$data) {
            exception('获取模拟企业小时数据失败！');
        }
        $dataNew['drain_id']      = $data['drain_id'];
        $dataNew['so2_emissions'] = $data['so2_emissions'];
        $dataNew['nox_emissions'] = $data['nox_emissions'];
        return $dataNew;
    }

    // +----------------------------------------------------------------------
    // | 公司
    // +----------------------------------------------------------------------
    /**
     * （减排前）排放量 (计算方式) - 方式1
     * @param int $days 天数
     * @param int $companyId 公司ID
     * @param string $type 污染物类型
     * @return float|int
     * @throws \Exception
     * @author hongwei 2019-07-16
     */
    public function getSimulationEmissionWayOne($days, $companyId, $type)
    {
        //step1:获取烟气量
        $gas     = new SimulationGasOutService();
        $flueGas = $gas->getBeforeFlueGas($companyId, $days);

        //step2:获取运行浓度
        $concentration = new SimulationConcentrationService();
        $concentration = $concentration->getBeforeSimulationConcentrationByCompanyId($companyId, $type);

        //step3:获取排放量
        $formula = new FormulaService();
        $result  = $formula->emissionFormula($flueGas, $concentration);
        if (!$result) {
            return false;
        }
        return $result;
    }

    /**
     * 单个公司 - 排放量 - 小时平均（方式二：SQL方式）
     * @param $companyId
     * @return array
     * @throws \Exception
     * @author hongwei 2019-07-25
     */
    public function getSimulationSingleCompanyEmissionHourAvg($companyId)
    {
        $sql = 'SELECT company_id, company_name,';
        $sql .= 'sum(emissions_so2)/24 AS so2_emissions,';
        $sql .= 'sum(emissions_nox)/24 AS nox_emissions,';
        $sql .= 'sum(emissions_smoke)/24 as tsp_emissions ';
        $sql .= 'FROM `company_industry_monitor_simulation_hour_avg`  where company_id = ' . (int)$companyId;
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetch();
        if (!$data) {
            exception('获取模拟企业单个企业小时排放量数据失败！');
        }
        $dataNew['company_id']    = $data['company_id'];
        $dataNew['company_name']  = $data['company_name'];
        // $dataNew['so2_emissions'] = $data['so2_emissions'];
        // $dataNew['nox_emissions'] = $data['nox_emissions'];
        // $dataNew['tsp_emissions'] = $data['tsp_emissions'];

        // 毫克转吨 chenge by lichao 2019-08-08
        $dataNew['so2_emissions'] = $data['so2_emissions'] / pow(10, 9);
        $dataNew['nox_emissions'] = $data['nox_emissions'] / pow(10, 9);
        $dataNew['tsp_emissions'] = $data['tsp_emissions'] / pow(10, 9);
        return $dataNew;
    }

    /**
     * +----------------------------------------------------------
     * 1.4 单个公司 - 排放量
     * 获取模拟企业 - 排口 - 小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param $companyId
     * @param $emissionUnit
     * +----------------------------------------------------------
     * @return mixed
     * @throws \Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getSimulationSingleCompanyEmissionHourAvg2($companyId, $emissionUnit = 'kg')
    {
        $formula    = new FormulaService();
        $totalHours = 24 * $formula->getUnit($emissionUnit);
        $sql        = 'SELECT a.company_id,a.company_name,sum(a.emissions_so2)/' . $totalHours . ' AS so2_emissions,sum(a.emissions_nox)/' . $totalHours . ' AS nox_emissions,sum(a.emissions_smoke)/' . $totalHours . ' as tsp_emissions FROM `company_industry_monitor_simulation_hour_avg` as a left join basic_company as b on a.company_id = b.id where company_id = ' . (int)$companyId . ' and b.delete_time = 0  group by a.company_id order by a.company_id asc';
        $data       = $this->pdoFetchAll($sql);
        if (!$data) {
            exception('获取小时数据失败！');
        }
        return $data;
    }

    // +----------------------------------------------------------------------
    // | 行业
    // +----------------------------------------------------------------------
    /**
     * 单个行业 - 排放量 - 小时平均（SQL方式） - 区分污染物
     * @param int $industryId 行业ID
     * @param string $type 污染物类型
     * @return array
     * @throws \Exception
     * @author hongwei 2019-07-26
     */
    public function getSimulationSingleIndustryEmissionHourAvg($industryId, $type)
    {
        $sql = 'SELECT industry_id, ';
        $sql .= 'sum(emissions_so2)/24 AS so2_emissions,';
        $sql .= 'sum(emissions_nox)/24 AS nox_emissions,';
        $sql .= 'sum(emissions_smoke)/24 as tsp_emissions ';
        $sql .= 'FROM `company_industry_monitor_simulation_hour_avg`  where industry_id = ' . (int)$industryId;
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetch();
        if (!$data) {
            exception('获取模拟企业单个企业小时排放量数据失败！');
        }
        $dataNew['industry_id'] = $data['industry_id'];
        if (strtoupper($type) == 'SO2') {
            $dataNew['emissions'] = $data['so2_emissions'];
        }
        if (strtoupper($type) == 'NO2') {
            $dataNew['emissions'] = $data['nox_emissions'];
        }
        return $dataNew;
    }

    /**
     * 每个行业 - 排放量 - 小时平均（SQL方式） - 区分污染物
     * @return array
     * @throws \Exception
     * @author hongwei 2019-07-26
     */
    public function getSimulationEveryIndustryEmissionHourAvg()
    {
        $sql = 'SELECT industry_id, ';
        $sql .= 'sum(emissions_so2)/24 AS so2_emissions,';
        $sql .= 'sum(emissions_nox)/24 AS no2_emissions,';
        $sql .= 'sum(emissions_smoke)/24 as tsp_emissions ';
        $sql .= 'FROM `company_industry_monitor_simulation_hour_avg` group by  industry_id';
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();
        if (!$data) {
            return false;
        }
        $newData = [];
        foreach ($data as $k => $v) {
            $newData[$k]['industry_id']   = $v['industry_id'];
            $newData[$k]['so2_emissions'] = $v['so2_emissions'];
            $newData[$k]['no2_emissions'] = $v['no2_emissions'];
        }
        return $newData;
    }

    // +----------------------------------------------------------------------
    // | （一）获取模拟企业 小时排放量
    // +----------------------------------------------------------------------
    /**
     * +----------------------------------------------------------
     * 1.1 所有公司 - 所有排口 - 排放量
     * 获取模拟企业 - 小时平均排放量 - 平均值(默认前七天)
     * @param $emissionUnit
     * +----------------------------------------------------------
     * @return mixed
     * @throws \Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getSimulationAllCompanyDrainEmissionHourAvg($emissionUnit = 'kg')
    {
        $formula    = new FormulaService();
        $totalHours = 24 * $formula->getUnit($emissionUnit);
        $sql        = 'SELECT a.company_id,a.company_name,a.drain_id,a.drain_name,sum(a.emissions_so2)/' . $totalHours . ' AS so2_emissions,sum(a.emissions_nox)/' . $totalHours . ' AS nox_emissions,sum(a.emissions_smoke)/' . $totalHours . ' as tsp_emissions FROM `company_industry_monitor_simulation_hour_avg` as a left join basic_company as b on a.company_id = b.id  where a.drain_id <> 0 and b.delete_time = 0 group by a.drain_id order by a.drain_id asc';
        $sth        = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();
        if (!$data) {
            exception('获取排口小时数据失败！');
        }
        $data = field_as_key($data, 'drain_id');
        return $data;
    }

    /**
     * 获取指定排口的排放量
     * @param $drainIds
     * @param string $emissionUnit
     * @return array
     * @throws \Exception
     */
    public function getSimulationControlDrainIdsEmissionTypeHourAvg($drainIds, $type = 'SO2', $emissionUnit = 'kg')
    {
        $formula    = new FormulaService();
        $totalHours = 24 * $formula->getUnit($emissionUnit);
        $sql        = 'SELECT drain_id,drain_name, ';
        if (strtoupper($type) == 'SO2') {
            $sql        .= 'sum(emissions_so2)/' . $totalHours . ' AS emissions ';
        }
        if (strtoupper($type) == 'NO2') {
            $sql        .= 'sum(emissions_nox)/' . $totalHours . ' AS emissions ';
        }
        $sql        .= 'FROM `company_industry_monitor_simulation_hour_avg` ';
        $sql        .= 'where drain_id <> 0  ';
        $sql        .= 'and drain_id in ('.$drainIds.') ';
        $sql        .= 'group by drain_id order by drain_id asc';
        $sth        = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();
        if (!$data) {
            exception('获取数据失败！'.__METHOD__);
        }
        return $data;
    }

    /**
     * +----------------------------------------------------------
     * 1.1 相关行业 - 所有公司 - 所有排口 - 排放量
     * 获取模拟企业 - 小时平均排放量 - 平均值(默认前七天)
     * @param int $industryId 行业ID
     * @param string $emissionUnit 单位
     * +----------------------------------------------------------
     * @return mixed
     * @throws \Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getSimulationAllIndustryCompanyDrainEmissionHourAvg($industryId, $emissionUnit = 'kg')
    {
        $formula    = new FormulaService();
        $totalHours = 24 * $formula->getUnit($emissionUnit);
        $sql        = 'SELECT a.industry_id,a.company_id,a.company_name,a.drain_id,a.drain_name,sum(a.emissions_so2)/' . $totalHours . ' AS so2_emissions,sum(a.emissions_nox)/' . $totalHours . ' AS nox_emissions,sum(a.emissions_smoke)/' . $totalHours . ' as tsp_emissions FROM `company_industry_monitor_simulation_hour_avg` as a left join basic_company as b on a.company_id = b.id  where a.drain_id <> 0 and b.delete_time = 0 and a.industry_id = ' . $industryId . ' group by a.drain_id order by a.drain_id asc';
        $sth        = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();
        if (!$data) {
            exception('获取行业排口小时数据失败！');
        }
        $data = field_as_key($data, 'drain_id');
        return $data;
    }

    /**
     * +----------------------------------------------------------
     * 1.2 所有公司 - 排放量
     * 获取模拟企业 - 小时平均排放量 - 平均值(默认前七天)
     * @param $emissionUnit
     * +----------------------------------------------------------
     * @return mixed
     * @throws \Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getSimulationAllCompanyEmissionHourAvg($emissionUnit = 'kg')
    {
        $formula    = new FormulaService();
        $totalHours = 24 * $formula->getUnit($emissionUnit);
        $sql        = 'SELECT a.company_id,a.company_name,sum(a.emissions_so2)/' . $totalHours . ' AS so2_emissions,sum(a.emissions_nox)/' . $totalHours . ' AS nox_emissions,sum(a.emissions_smoke)/' . $totalHours . ' as tsp_emissions FROM `company_industry_monitor_simulation_hour_avg` as a left join basic_company as b on  a.company_id = b.id where b.delete_time = 0 group by a.company_id order by a.company_id asc';
        $sth        = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();
        if (!$data) {
            exception('获取公司小时数据失败！');
        }
        return $data;
    }

    /**
     * +----------------------------------------------------------
     * 1.3 单个公司 - 所有排口 - 排放量
     * 获取模拟企业 - 排口 - 小时平均排放量 - 平均值(默认前七天)
     * +----------------------------------------------------------
     * @param $companyId
     * @param $emissionUnit
     * +----------------------------------------------------------
     * @return mixed
     * @throws \Exception
     * @author hongwei 2019-06-27
     * +----------------------------------------------------------
     */
    public function getSimulationSingleCompanyDrainEmissionHourAvg($companyId, $emissionUnit = 'kg')
    {
        $formula    = new FormulaService();
        $totalHours = 24 * $formula->getUnit($emissionUnit);
        $sql        = 'SELECT company_id,company_name,drain_id, drain_name, sum(emissions_so2)/' . $totalHours . ' AS so2_emissions,sum(emissions_nox)/' . $totalHours . ' AS nox_emissions,sum(emissions_smoke)/' . $totalHours . ' as tsp_emissions FROM `company_industry_monitor_simulation_hour_avg`  where company_id = ' . (int)$companyId . ' group by drain_id order by drain_id asc';
        $sth        = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();
        if (!$data) {
            exception('获取小时数据失败！');
        }
        $data = field_as_key($data, 'drain_id');
        return $data;
    }

    /**
     * +----------------------------------------------------------
     * 获取单个行业 - 模拟企业 - 天数据
     * +----------------------------------------------------------
     * @param int $industryId 行业ID
     * @param string $emissionUnit 单位
     * +----------------------------------------------------------
     * @return array
     * @throws \Exception
     * @author hongwei 2019-07-06
     * +----------------------------------------------------------
     */
    public function getSimulationSingleIndustryEmissionDayAvg($industryId, $emissionUnit = 't')
    {
        $formula    = new FormulaService();
        $totalHours = $formula->getUnit($emissionUnit);
        $sql        = 'SELECT a.industry_id,sum(a.emissions_so2_avg)/' . $totalHours . ' AS so2_emissions,sum(a.emissions_nox_avg)/' . $totalHours . ' AS nox_emissions,sum(a.emissions_smoke_avg)/' . $totalHours . ' as tsp_emissions FROM `company_industry_monitor_simulation_day_avg` as a left join basic_company as b on a.company_id = b.id  where b.delete_time = 0 and a.industry_id = ' . $industryId . ' group by a.industry_id order by a.industry_id asc';
        $sth        = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();
        if (!$data) {
            exception('获取行业天数据失败！');
        }
        return $data;
    }

}